package com.meichis.yndms.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.MarkerOptions;
import com.meichis.mcsappframework.utils.Tools;
import com.meichis.yndms.R;
import com.meichis.yndms.databinding.ActivityClientLocationBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.ClientLocationVM;

public class ClientLocationActivity extends BaseActivity {

    private ActivityClientLocationBinding binding;
    private ClientLocationVM vm;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_client_location);
        vm = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(ClientLocationVM.class);
        binding.setLifecycleOwner(this);
    }


    @Override
    protected void init() {

        binding.navigation.txtTitle.setText("店铺位置");
        binding.navigation.navBack.setOnClickListener((view) -> close());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding.map.onCreate(savedInstanceState);
        vm.initMap(binding.map.getMap());

        View view = View.inflate(this, R.layout.layout_marker, null);
        TextView tvName = view.findViewById(R.id.tv_name);
        tvName.setText(vm.getShopInfo().getValue().getFullName());
        Bitmap bitmap = Tools.convertViewToBitmap(view);
       // LatLng latLng = new LatLng(vm.getShopInfo().getValue().getl(), vm.getClientInfo().getValue().getLongitude());
        binding.map.getMap().addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
               // .position(latLng)
                .title(vm.getShopInfo().getValue().getAddress()).draggable(true).snippet("").zIndex(1)
                .icon(BitmapDescriptorFactory.fromBitmap(bitmap)).visible(true));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        binding.map.onSaveInstanceState(outState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (binding.map != null) {
            //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
            binding.map.onResume();
        }
    }

    @Override
    protected void onPause() {
        if (binding.map != null) {
            //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
            binding.map.onPause();
        }
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        if (binding.map != null) {
            //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
            binding.map.onDestroy();
        }
        super.onDestroy();

    }
}